package com.modern.js.controller;

import com.modern.js.entity.Business;
import com.modern.js.entity.Role;
import com.modern.js.pojo.OrdersAssist;
import com.modern.js.pojo.RoleAssist;
import com.modern.js.service.*;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

@Controller
@RequestMapping("/statistics")
public class StatisticsController {

    @Autowired
    private IEmployeeService iEmployeeService;
    @Autowired
    private IDepartmentService iDepartmentService;
    @Autowired
    private ICustomerService iCustomerService;
    @Autowired
    private IOrdersService iOrdersService;
    @Autowired
    private IRoleService iRoleService;
    @Autowired
    private AssistPojoService assistPojoService;
    @Autowired
    private IBusinessService iBusinessService;

    @GetMapping("/list")
    public String selectList(Model model){
        //        员工信息统计
        Integer empNum = iEmployeeService.count();
        //        部门信息统计
        Integer deptNum = iDepartmentService.count();
        //        客户信息统计
        Integer custNum = iCustomerService.count();
        //        订单信息统计
        Integer orderNum = iOrdersService.count();
        //        批发信息统计
        Integer buNum = iBusinessService.count();

        Map<String,Object> map = new HashMap<>();
        map.put("empNum",empNum);
        map.put("deptNum",deptNum);
        map.put("custNum",custNum);
        map.put("orderNum",orderNum);
        map.put("buNum",buNum);
        model.addAttribute("map",map);

        return"welcome";
    }


    /**
     * 部门员工数据
     * @return
     */
    @GetMapping("/empStatistics")
    @ResponseBody
    public Map empStatistics(){
        Map<String,Object> map = new HashMap<String,Object>();
        List<Role> list = iRoleService.list();
        List<RoleAssist> roleList = new ArrayList<>();
        List<Object> listSum = new ArrayList<>();
        List<Object> listRole = new ArrayList<>();
        for (int i=0;i<list.size();i++){
            listSum.add(list.get(i).getRoleName());
        }
        for (int i=0;i<list.size();i++){
            RoleAssist roleAssist = new RoleAssist();
            Integer roleId = list.get(i).getRoleId();
            roleAssist=assistPojoService.selectCount(roleId);
            roleList.add(roleAssist);
        }

        for (int i = 0;i<roleList.size();i++){
            System.out.println("数值："+roleList.get(i).getCount());
            listRole.add(roleList.get(i).getCount());
        }
        map.put("listRole",listRole);
        map.put("listSum",listSum);

        return map;
    }

    /**
     * 订单数据
     * @return
     */
    @GetMapping("/orderStatistics")
    @ResponseBody
    public Map selectCount(){
        Map<String,Object> map = new HashMap<String, Object>();
        List<Business> list = iBusinessService.list();
        List<OrdersAssist> orderList = new ArrayList<>();
        List<Object> busList = new ArrayList<>();
        List<Object> list2 = new ArrayList<>();

        for (Business business:list){
            System.out.println("这是值："+business.getBusubessName());
            busList.add(business.getBusubessName());
            OrdersAssist ordersAssist = new OrdersAssist();
            ordersAssist = iOrdersService.selectCount(business.getBusubessName());
            orderList.add(ordersAssist);
        }
        for (OrdersAssist ordersAssist:orderList){
            list2.add(ordersAssist.getCount());
        }

        map.put("busList",busList);
        map.put("list2",list2);
        return map;
    }
}
